﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Globalization;

namespace TST.SharePointObjects
{
    public class Licensing
    {
        private LicensingMode _mode;
        private DateTime _endDate;

        internal LicensingMode LicensingMode
        {
            get { return _mode; }
        }

        internal DateTime EndDate
        {
            get { return _endDate; }
        }

        internal Licensing(string key)
        {
            if (string.IsNullOrEmpty(key))
            {
                _mode = LicensingMode.None;
                return;
            }
            XmlDocument licenseDoc = new XmlDocument();
            try
            {
                licenseDoc.LoadXml(key);
                string licenseKey = string.Empty;
                string licensedTo = string.Empty;
                DateTime endDate = DateTime.MaxValue;
                foreach (XmlAttribute att in licenseDoc.DocumentElement.Attributes)
                {
                    switch (att.Name)
                    {
                        case "key":
                            licenseKey = att.Value;
                            break;
                        case "licensedto":
                            licensedTo = att.Value;
                            break;
                        case "enddate":
                            if (!DateTime.TryParse(att.Value, new CultureInfo(1033), System.Globalization.DateTimeStyles.None, out endDate))
                            {
                                _mode = LicensingMode.Unknown;
                            }
                            break;
                    }
                }
                if (string.IsNullOrEmpty(licenseKey) || string.IsNullOrEmpty(licensedTo))
                {
                    _mode = LicensingMode.Demo;
                    _endDate = endDate;
                    if (_endDate < DateTime.Now)
                    {
                        _mode = LicensingMode.Expired;
                    }
                }
                else if (KeyIsValid(licenseKey))
                {
                    _mode = LicensingMode.FullLicense;
                    _endDate = DateTime.MaxValue;
                }
                else
                {
                    _mode = LicensingMode.None;
                    _endDate = DateTime.MinValue;
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteError(string.Format("Error while getting license information: {0}.", ex.Message), "Licensing.Licensing");
                _mode = LicensingMode.Unknown;
                throw ex;
            }

        }

        private static bool KeyIsValid(string key)
        {
            return !string.IsNullOrEmpty(key);
        }

        internal static object GetLicenseXML(int version)
        {
            string result = string.Empty;
            //if (version == 1)
            //{
                result = string.Format("<solicense key=\"ok\" licensedto=\"demo@sharepointobjects.com\" enddate=\"{0}\"></solicense>", DateTime.Now.AddMonths(2).ToString(new CultureInfo(1033)));
            //}
            return result;
        }
    }

    public enum LicensingMode
    {
        None,
        Demo,
        Expired,
        FullLicense,
        Unknown
    }
}
